const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      rules: {
        Start_Date: [{ required: true, message: '请选择开始时间', trigger: 'blur' }],
        End_Date: [{ required: true, message: '请选择结束时间', trigger: 'blur' }],
        Supplier_SerialNumber: [{ required: true, message: '请输入供应商流水号', trigger: 'blur' }],
      },
      article_info: [],
      isDisabled: false,
      form: {     //批发价格单
        Wholesale_Unit_Price_SerialNumber: "",  //主键
        Start_Date: '',
        End_Date: '',
        Article_SerialNumber: '',   //商品标识
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        Children: {
          wholesale_unitprice_detail: [{
            Wholesale_Unit_Price_Detail_SerialNumber: '',
            Wholesale_Unit_Price_SerialNumber: '',  //外键
            Quantity_Interval: '',    //数量区间
            Unit_Price: '',   //价格
            StartNum: '',
            EndNum: ''
          }],
        }
      },
      loading: false
    }
  },
  mounted() {
    this.getData()
    console.log($.cookie("enterprise"))
  },
  methods: {
    getData() {
      let _this = this;
      $.ajax({
        url: DomainName + '/GoodsFileInquiry',
        type: 'POST',
        data: { Data: "supplier_info" },
        success: function (res) {
          _this.supplier_info = res
        }
      })
      $.ajax({
        url: DomainName + '/ArticleInfo',
        type: 'GET',
        data: { action: "jQueryAjax" },
        success: function (res) {
          res = res.filter(function (item) {
            return item.Row_Status != '1'
          })
          _this.article_info = res
        }
      })
    },
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.form.Children.wholesale_unitprice_detail.forEach(item => {
            if (item.StartNum > item.EndNum) {
              return _this.$message.error('开始数量不能大于结束数量')
            } else {
              item.Quantity_Interval = item.StartNum + '-' + item.EndNum
              _this.loading = true
              console.log(_this.form)
              $.ajax({
                url: DomainName + '/GoodsFileAdd',
                type: 'POST',
                data: {
                  Data: JSON.stringify({
                    wholesale_unitprice: [_this.form]
                  }),
                  token: $.cookie("token")
                },
                success: function (res) {
                  if (res.Status === 'success') {
                    _this.$message.success("提交成功")
                    window.location.href = './wholesalePrice.html'
                  } else {
                    _this.$message.error("提交失败，请稍后再试")
                  }
                },
                error: function () {
                  _this.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                  _this.loading = false
                  if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                    window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                  }
                }
              })
            }
          })

        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    Change_Unit_Price(index) {
      console.log(index)
      this.form.Article_SerialNumber = index
      // this.isDisabled = true
      // let str = this.form.Children.purchase_unitprice_detail[index].Article_SerialNumber
      // for (let i = 0; i < this.article_info.length; i++) {
      //   if (this.article_info[i].Article_SerialNumber == str) {
      //     this.form.Children.purchase_unitprice_detail[index].Unit_Price = this.article_info[i].Unit_Price
      //   }
      // }
    },
    add_wholesale_unitprice_detail() {
      this.form.Children.wholesale_unitprice_detail.push({     //采购价格单
        Wholesale_Unit_Price_Detail_SerialNumber: '',
        Wholesale_Unit_Price_SerialNumber: '',  //外键
        Quantity_Interval: '',    //数量区间
        Unit_Price: '',   //价格
        StartNum: '',
        EndNum: ''
      })
    },
    remove_wholesale_unitprice_detail() {
      if (this.form.Children.wholesale_unitprice_detail.length > 1) {
        this.form.Children.wholesale_unitprice_detail.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
})